package com.alibaba.shopping_order_service.service;

import com.alibaba.shopping_common.pojo.Address;
import com.alibaba.shopping_common.pojo.Area;
import com.alibaba.shopping_common.pojo.City;
import com.alibaba.shopping_common.pojo.Province;
import com.alibaba.shopping_common.service.AddressService;
import com.alibaba.shopping_order_service.mapper.AddressMapper;
import com.alibaba.shopping_order_service.mapper.AreaMapper;
import com.alibaba.shopping_order_service.mapper.CityMapper;
import com.alibaba.shopping_order_service.mapper.ProvinceMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AddressServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/18 16:39
 */
@Service
@DubboService
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询所有的省份数据
     * @return
     */
    @Override
    public List<Province> findAllProvince() {
        return provinceMapper.selectList(null);
    }

    /**
     * 查询省份下面所有的城市
     * @param provinceId
     * @return
     */
    @Override
    public List<City> findCityByProvince(Long provinceId) {
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.eq("provinceId",provinceId);
        return cityMapper.selectList(wrapper);
    }

    /**
     * 查询市下面所有的区县
     * @param cityId
     * @return
     */
    @Override
    public List<Area> findAreaByCity(Long cityId) {
        // 构造查询条件
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("cityId",cityId);
        // 执行查询
        return areaMapper.selectList(wrapper);
    }

    /**
     * 新增收货地址
     * @param address
     */
    @Override
    public void addAddress(Address address) {
        addressMapper.insert(address);
    }

    /**
     * 删除收货地址
     * @param id
     */
    @Override
    public void delAddress(Long id) {
        addressMapper.deleteById(id);
    }

    /**
     * 更新收货地址
     * @param address
     */
    @Override
    public void updateAddress(Address address) {
        addressMapper.updateById(address);
    }

    /**
     * 根据ID查询收货地址
     * @param id
     * @return
     */
    @Override
    public Address findById(Long id) {
        return addressMapper.selectById(id);
    }

    /**
     * 查询用户所有的收货地址
     * @param userId
     * @return
     */
    @Override
    public List<Address> findByUserId(Long userId) {
        // 构造查询条件
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        // 执行查询
        return addressMapper.selectList(wrapper);
    }
}




